<?php
/*
 * Created on 2007.01.23.
 */


require_once "pear/XML/Unserializer.php";
require_once "pear/XML/Serializer.php";


class UserConfig {
	
	var $configfile = "./user/user.xml";
	var $hideadmins = false;
	var $defaultUser = "admin";
	
  	function loginUser($name, $pass ) {
  		//
		$users = $this->_readUserList();
		
		foreach ( $users['user'] as $user){
			if ($user['name'] == $name && $user['pass']==$pass ) $userObj = $user;
		}
		if (!isset($userObj)) {
			return array("valid"=>0,"error"=>"Incorrect user name or password!", "code"=>"login");
		}
  		else { 
			if(!session_id()) session_start();
			$_SESSION['flashcommander_username'] = $userObj['name'];
			return array("valid"=>1,"user"=>$userObj);
  		}
	}
	
  	function checkSession() {
  		//
  		$userObj = $this->_getUser();
  		
		if (!isset($userObj)) {
			return array("valid"=>0,"error"=>"Incorrect user name or password!", "code"=>"login");
		}
  		else { 
			return array("valid"=>1,"user"=>$userObj);
  		}
	}
	
	
	function logoutUser( ){
		$_SESSION['flashcommander_username'] = null;
		if(isset($_SESSION['flashcommander_username']))	unset($_SESSION['flashcommander_username']);
		return "logged out";
	}
	
	// ******************************   PRIVATE METHODS
	
	function _hasPermission($userObj, $perm){
		$permArr = explode(';', $userObj['permissions']);
		return in_array($perm, $permArr);
	}
	
	function _readUserList(){
		
		$unserializer_options = array('forceEnum' => array('user'), 'parseAttributes'=>true );
		$unserializer = &new XML_Unserializer($unserializer_options);
		$result = $unserializer->unserialize( dirname(__FILE__) ."/" . $this->configfile, true);
		$users = $unserializer->getUnserializedData();
		return $users;		
	}
	
	function _writeUserList($users){
		$serializer_options = array (
		   'addDecl' => TRUE,
		   'encoding' => 'utf-8',
		   'indent' => '  ',
		   'rootName' => 'users',
		   'mode' => 'simplexml',
		  'defaultTagName' => 'user',
		  'scalarAsAttributes'=> array('uploadfilter'=>true)
		);
		$Serializer = &new XML_Serializer($serializer_options);
		$status = $Serializer->serialize($users);
		$data = $Serializer->getSerializedData();
		$handle = fopen( dirname(__FILE__)."/" . $this->configfile, 'w');
		fwrite($handle, $data); 
		fclose($handle);		
		
	}
	
	function _hidePasswords($users){
		for ($i=0; $i< count($users['user']); $i++ ){
			$user = &$users['user'][$i];
			unset($user['pass']);
		}		
		return $users;
	}

	
	function _getUser(){
		$name = $_SESSION['flashcommander_username'];
		if (!isset($name)){
			exit('User not logged in!');	
		}
		
		$users = $this->_readUserList();
		
		foreach ( $users['user'] as $user){
			if ($user['name'] == $name ) $userObj = $user;
		}
		
		return $userObj;
	}
	
	function _pathToUsers($path){
		$users = $this->_readUserList();
		
		$result = array();
		
		for ($i=0; $i< count($users['user']); $i++ ){
			$user = &$users['user'][$i];
			$rootdir = realpath($user['rootdir']);
			$path = realpath($path);
			
			if ( substr($path, 0, strlen($rootdir))== $rootdir) {
				array_push($result,$user);
			}
		}
		
		return $result;		
	}
	
	// ****************************************** PUBLIC METHODS
	
	function updateAccount($oldpass, $newpass, $email){
		$myuser = $this->_getUser();
		
		if ($oldpass!=$myuser['pass'] )
			return array('valid'=>0, 'error'=>'You have to provide your old password!', "code"=>"oldpass");
		
		$users = $this->_readUserList();
		
		for ($i=0; $i< count($users['user']); $i++ ){
			$user = &$users['user'][$i];
			if ($user['name'] == $myuser['name'] && $user['pass']==$myuser['pass'] ) {
				$user['pass'] = $newpass;
				$user['email'] = $email;
				$userObj = $user;
			}
		}
		
		$this->_writeUserList($users);
		
		unset($userObj['pass']); 
		return array('valid'=>1, 'user'=>$userObj);	
	}
	
	function getUsers(){
		$myuser = $this->_getUser();
		if (!$this->_hasPermission($myuser,'users')) 
			return array('valid'=>0, 'error'=>'You don\'t have permission to get user list!');

		$users = $this->_readUserList();
		
		$users = $this->_hidePasswords($users);
		return array('valid'=>1, 'list'=>$users['user']);
	}	
	
	
	function addUser($userObj){
		$myuser = $this->_getUser();
		if (!$this->_hasPermission($myuser,'users')) 
			return array('valid'=>0, 'error'=>'You don\'t have permission to add users!');
		
		
		$users = $this->_readUserList();
		
		foreach ( $users['user'] as $user){
			if ($user['name'] == $userObj['name'] ) 
				return array('valid'=>0, 'error'=>'User name already exists!');
		}
		
 		$rootdir = realpath($myuser['rootdir']);
 		$actdir = realpath($userObj['rootdir']);
 		if ($rootdir != substr($actdir, 0, strlen( $rootdir ))) 
 			return array("valid"=>0,"error"=>"The specified root directory is out of reach!");
		
		array_push($users['user'],$userObj);
		$this->_writeUserList($users);
		
		$users = $this->_hidePasswords($users);
		return array('valid'=>1, 'list'=>$users['user']);
	}
	
	function delUser($name){
		if ($name=='admin') 
			return array('valid'=>0, 'error'=>'You cannot delete the administrator!');
		
		$myuser = $this->_getUser();
		if (!$this->_hasPermission($myuser,'users')) 
			return array('valid'=>0, 'error'=>'You don\'t have permission to delete users!');
		
		$users = $this->_readUserList();
		
		for ($i=0; $i< count($users['user']); $i++ ){
			$user = &$users['user'][$i];
			if ($user['name'] == $name) $index = $i;
		}
		if (isset($index)) array_splice($users['user'],$index,1);
		
		$this->_writeUserList($users);
		
		$users = $this->_hidePasswords($users);
		return array('valid'=>1, 'list'=>$users['user']);
	}
	
	function editUser($userObj){
		$myuser = $this->_getUser();
		if (!$this->_hasPermission($myuser,'users')) 
			return array('valid'=>0, 'error'=>'You don\'t have permission to delete users!');
		
		$users = $this->_readUserList();
		
		for ($i=0; $i< count($users['user']); $i++ ){
			$user = &$users['user'][$i];
			if ($user['name'] == $userObj['name']) {
				$user['email'] = $userObj['email'];
				$user['rootdir'] = $userObj['rootdir'];
				$user['permissions'] = $userObj['permissions'];
				$user['uploadfilter'] = $userObj['uploadfilter'];
			}
		}
		
		$this->_writeUserList($users);
		
		$users = $this->_hidePasswords($users);
		return array('valid'=>1, 'list'=>$users['user']);
	}

	function setNewPass($name, $newpass){
		$myuser = $this->_getUser();
		if (!$this->_hasPermission($myuser,'users')) 
			return array('valid'=>0, 'error'=>'You don\'t have permission to change passwords!');
		
		$users = $this->_readUserList();
		
		for ($i=0; $i< count($users['user']); $i++ ){
			$user = &$users['user'][$i];
			if ($user['name'] == $name ) {
				$user['pass'] = $newpass;
			}
		}
		$this->_writeUserList($users);
		
		return array('valid'=>1);	
	}
	
}
?>